<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="订单号">
          <el-input v-model="form.order_no" placeholder="订单号" />
        </el-form-item>
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="支付状态">
          <el-select v-model="form.status" placeholder="状态">
            <el-option
              v-for="item in statusList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户UID" align="center" width="120" />
          <el-table-column prop="order_no" label="订单号" align="center" width="200" />
          <el-table-column prop="goods_id" label="商品ID" align="center" width="150" />
          <el-table-column prop="goods_name" label="商品名称" align="center" width="150"/>
          <el-table-column prop="status" label="订单状态" align="center" />
          <el-table-column prop="pay_method" label="付款方式" align="center" />
          <el-table-column prop="diamond" label="钻石数量" align="center" />
          <el-table-column prop="quantity" label="下单数量" align="center" />
          <el-table-column prop="base_price" label="下单价格" align="center" />
          <el-table-column prop="base_amount" label="下单金额" align="center" />
          <el-table-column prop="base_currency" label="币种" align="center" />
          <el-table-column prop="real_price" label="实际价格" align="center" />
          <el-table-column prop="real_amount" label="实际金额" align="center" />
          <el-table-column prop="real_currency" label="实际币种" align="center" />
          <el-table-column prop="createtime" label="下单时间" align="center" :formatter="formatterTime" width="180" />
          <el-table-column prop="paytime" label="付款时间" align="center" width="180" :formatter="formatterTime" />
          <el-table-column prop="channel" label="APP渠道" align="center" />
          <el-table-column prop="out_order_no" label="三方订单号" align="center" width="180" />
          <el-table-column prop="order_ext" label="扩展信息" align="center" />
          <el-table-column prop="app_mark" label="APP标记" align="center" />
          <el-table-column prop="ip" label="IP" align="center" width="180" />
        </el-table>
        <div class="table-pagination">
          <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize" :page-sizes="[10, 25, 50, 100]"
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="pageable.total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";

import { formatterTime, exportToExcel, exportOrDownload, timestamp, pickerDefaultTime } from "@/utils/utils";
import {
  rechargeOrderListApi,
} from "@/api/moby/order/order";

const tableData = ref([]);
const statusList = ref([]);
const formData = {
  order_no: "",
  timeData: [],
  uid: "",
  status: ""
};
const form = reactive({ ...formData });

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_no: form.order_no,
    uid: form.uid,
    status: form.status,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

const loading = ref(false);

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await rechargeOrderListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    statusList.value = res.data.extra.status_list
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await rechargeOrderListApi(data, "blob");
  exportOrDownload(res, "Mobybox-充值订单列表");
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
